//
//  PageViewImageCell.swift
//  
//
//  Created by Ralf on 6/12/15.
//
//

import UIKit

class PageViewImageCell: TableViewCell, UIScrollViewDelegate
{
    let __preview_image_view_tag = 10
    let __favorites_button_tag   = 30
    let __scroll_view_tag        = 100
    let __pagecontrol_tag        = 101
    var images                   = [UIImageView]()

    var activity: NSMutableDictionary!

    override func awakeFromNib()
    {
        super.awakeFromNib()

        //        initPreviewImageView()
        initScrollView()
        initPageControl()
    }

    override func setSelected(selected: Bool, animated: Bool)
    {
        super.setSelected(selected, animated: animated)
    }

    func setData(data: NSMutableDictionary)
    {
        for imageView: UIImageView in images
        {
            imageView.removeFromSuperview()
        }

        images.removeAll(keepCapacity: false)

        activity = data
        let article   = data["article"] as! NSDictionary
        let imageURL  = article["imageURL"] as! String
        let imageView = UIImageView()
        imageView.load(appDelegate.parameterManager.server + "/" + imageURL, placeholder: nil)
        addImageView(imageView)
        //        setPreviewImageView(imageURL)
        let attachments = article["attachments"] as! [[String:AnyObject]]
        for attachment in attachments
        {
            let fileURL   = attachment["fileURL"] as! String
            let url       = appDelegate.parameterManager.server + "/" + fileURL
            let imageView = UIImageView()
            imageView.load(url, placeholder: nil)
            addImageView(imageView)
        }
    }

    func setPreviewImageView(imageURL: String)
    {
        let ivPreview = self.viewWithTag(__preview_image_view_tag) as! UIImageView
        ivPreview.load("\(appDelegate.parameterManager.server)/\(imageURL)", placeholder: nil)
    }

    func initPreviewImageView()
    {
        var imageView = self.viewWithTag(__preview_image_view_tag) as? UIImageView
        if (imageView == nil)
        {
            let frame = CGRect(x: 0, y: 0, width: screen.size.width, height: screen.size.width)
            imageView = UIImageView(frame: frame)
            imageView!.tag = __preview_image_view_tag
            imageView!.backgroundColor = UIColor.blackColor()
            self.addSubview(imageView!)
        }
    }

    @IBAction func favorites(sender: AnyObject)
    {
        if !appDelegate.isLogged()
        {
            login()
        }
        else
        {
            let button = self.viewWithTag(__favorites_button_tag) as! UIButton

            if !button.selected
            {
                activity["favorites"] = true
                let favoritesManager = FavoritesManager()
                let article          = activity["article"] as! NSDictionary
                let articleId        = String(article["id"] as! Int)
                favoritesManager.addFavorites(articleId)
                button.selected = true

                NSNotificationCenter.defaultCenter().postNotificationName("Refresh Activity", object: nil)
            }
        }
    }

    func initScrollView()
    {
        var scrollView = self.viewWithTag(__scroll_view_tag) as? UIScrollView

        if scrollView == nil
        {
            let frame = CGRectMake(0, 0, screen.size.width, screen.size.width)
            scrollView = UIScrollView(frame: frame)

            scrollView!.pagingEnabled = true
            scrollView!.showsHorizontalScrollIndicator = false
            scrollView!.showsVerticalScrollIndicator = false
            scrollView!.scrollsToTop = false
            scrollView!.delegate = self
            scrollView!.tag = __scroll_view_tag

            self.addSubview(scrollView!)
        }
    }

    func initPageControl()
    {
        var pageControl = self.viewWithTag(__pagecontrol_tag) as? UIPageControl

        if pageControl == nil
        {
            pageControl = UIPageControl()

            pageControl!.frame = CGRectMake(0, screen.size.width - 30, self.bounds.width, 20)
            pageControl!.pageIndicatorTintColor = UIColor.grayColor()
            pageControl!.backgroundColor = UIColor.clearColor()
            pageControl!.currentPageIndicatorTintColor = UIColor.whiteColor()
            pageControl!.tag = __pagecontrol_tag

            addSubview(pageControl!)
        }
    }

    func addImageView(imageView: UIImageView)
    {
        images.append(imageView)
        let scrollView  = self.viewWithTag(__scroll_view_tag) as! UIScrollView
        let pageControl = self.viewWithTag(__pagecontrol_tag) as! UIPageControl

        let width = screen.size.width * CGFloat(images.count)

        scrollView.contentSize = CGSizeMake(width, 0)

        let x = CGFloat(images.count - 1) * screen.size.width
        imageView.frame = CGRectMake(x, 0.0, screen.size.width, screen.size.width)
        scrollView.addSubview(imageView)

        pageControl.numberOfPages = images.count
        //        scrollView.contentOffset = CGPointMake(x, 0.0)
    }

    func scrollViewDidScroll(scrollView: UIScrollView)
    {
        let pageControl = self.viewWithTag(__pagecontrol_tag) as! UIPageControl

        let x = CGFloat(scrollView.contentOffset.x)
        let y = CGFloat(scrollView.frame.size.width / 2)

        let page = Int((x + y) / scrollView.frame.size.width)

        pageControl.currentPage = page
    }
}
